<?php
/**
 * CK 开发框架
 * User: Clake
 * Date: 15/6/13
 * Time: 15:49
 */

namespace CK\Core;


use CK\Template\Template;

class View extends Base {

    private static $_instance;

    /**
     * @var Template
     */
    private $_exp_tlp;

    public static function inst() {
        if (!isset(self::$_instance)) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    public function __construct() {
        $this->setTemplateEngine();
    }

    /**
     * 设置模板使用的变量
     * @param $key
     * @param $value
     */
    public function assign($key,$value) {
        $this->_exp_tlp->assign($key,$value);
    }

    /**
     * 设置VIEW的模板引擎
     */
    public function setTemplateEngine() {
        $conf = Config::inst('main')->get('template');
        $eng_tlp = Template::inst($conf);
        $this->_exp_tlp = $eng_tlp;
    }

    /**
     * 显示模板
     * @param $tpl
     *
     * @throws \CK\Ex\ExNotFound
     */
    public function display($tpl) {
        $this->_exp_tlp->display($tpl);
    }

    /**
     * 解释模板并返回模板内容
     * @param $tpl
     *
     * @return string
     */
    public function fetch($tpl) {
        return $this->_exp_tlp->fetch($tpl);
    }
}